/*
 * Copyright (c) 2014. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.jspx.txweb.view;

import com.jspx.sioc.annotation.Ref;
import com.jspx.txweb.Option;
import com.jspx.txweb.annotation.HttpMethod;
import com.jspx.txweb.annotation.Safety;
import com.jspx.txweb.dao.OptionDAO;
import com.jspx.txweb.env.TXWebIoc;
import com.jspx.txweb.table.OptionBundle;
import com.jspx.utils.StringUtil;

import java.util.List;
import java.util.Set;

/**
 * Created by yuan on 14-3-13.
 * 系统提供页面的默认字典库 调用接口
 */

public class OptionProvider implements Option {
    protected OptionDAO optionDAO;
    @Ref(name = TXWebIoc.dictionaryDAO)
    public void setDictionary(OptionDAO optionDAO) {
        this.optionDAO = optionDAO;
    }

    @Safety
    public void setNamespace(String namespace)
    {
        optionDAO.setNamespace(namespace);
    }

    /**
     *

     * @param mode 模式:0.模式使用名称作为值,1:模式使用code字段作为值
     * @return       生成选项字符串
     * */

    @Safety
    @SuppressWarnings("unchecked")
    public String getOptions(int mode) throws Exception
    {
        List<OptionBundle> optionBundleList = (List<OptionBundle>) optionDAO.getList(null,null,null, "sortType:A",1,500);
        StringBuilder sb = new StringBuilder();
        for (OptionBundle optionBundle : optionBundleList)
        {
            if (mode==0)
                sb.append(optionBundle.getName()).append(StringUtil.SEMICOLON);
            else
                sb.append(optionBundle.getCode()).append(":").append(optionBundle.getName()).append(StringUtil.SEMICOLON);
        }
        if (sb.length()>0) sb.setLength(sb.length()-1);
        return sb.toString();
    }

    @Safety
    @SuppressWarnings("unchecked")
    public String getOptionValue(String key,String namespace) throws Exception
    {
        optionDAO.setNamespace(namespace);
        OptionBundle optionBundle = optionDAO.getOptionValue(key);
        if (optionBundle==null) return StringUtil.empty;
        return optionBundle.getName();
    }

    @Safety
    public String getOptions(String namespace) throws Exception
    {
        return getOptions(0,namespace);

    }


    public String getOptions(int mode,String namespace) throws Exception
    {
        optionDAO.setNamespace(namespace);
        return getOptions(mode);
    }

    @Safety
    public String getSelected(String namespace) throws Exception
    {
        return getSelected(0,namespace);
    }

    /**
     *
     * @param mode 显示模式 0:得到名称 1:得到代码
     * @param namespace  命名空间
     * @return  得到选项
     * @throws Exception
     */
    public String getSelected(int mode,String namespace) throws Exception
    {
        optionDAO.setNamespace(namespace);
        OptionBundle optionBundle = optionDAO.getSelected();
        if (optionBundle ==null) return StringUtil.empty;
        return mode==0? optionBundle.getName(): optionBundle.getCode();
    }

    public Set<String> getSpaceSet()
    {
        return  optionDAO.getSpaceMap().keySet();
    }

    public String getCaption(String key)
    {
        return  optionDAO.getSpaceMap().get(key);
    }



}